<html>
<head><meta charset="utf-8"><title>inspecting ADT types on RHS of a const item · t-compiler/const-eval · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/index.html">t-compiler/const-eval</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/inspecting.20ADT.20types.20on.20RHS.20of.20a.20const.20item.html">inspecting ADT types on RHS of a const item</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="177650635"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/inspecting%20ADT%20types%20on%20RHS%20of%20a%20const%20item/near/177650635" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/inspecting.20ADT.20types.20on.20RHS.20of.20a.20const.20item.html#177650635">(Oct 08 2019 at 19:33)</a>:</h4>
<p>I'm poking at <a href="https://github.com/rust-lang/rust/issues/62614" target="_blank" title="https://github.com/rust-lang/rust/issues/62614">#62614</a> (just because I'm looking at other code related to structural_match)</p>



<a name="177651089"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/inspecting%20ADT%20types%20on%20RHS%20of%20a%20const%20item/near/177651089" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/inspecting.20ADT.20types.20on.20RHS.20of.20a.20const.20item.html#177651089">(Oct 08 2019 at 19:38)</a>:</h4>
<p>and I have to admit that I'm sort of flummoxed by something that I thought would be a relatively simple task</p>



<a name="177651498"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/inspecting%20ADT%20types%20on%20RHS%20of%20a%20const%20item/near/177651498" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/inspecting.20ADT.20types.20on.20RHS.20of.20a.20const.20item.html#177651498">(Oct 08 2019 at 19:43)</a>:</h4>
<p>My goal: I want to traverse the right-hand side of the const item, and check that any struct/enum I find there is <code>structural_match</code>. (I specifically <em>don't</em> want to walk the type, as discussed on <a href="https://github.com/rust-lang/rust/issues/62614" target="_blank" title="https://github.com/rust-lang/rust/issues/62614">#62614</a>)</p>



<a name="177651503"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/inspecting%20ADT%20types%20on%20RHS%20of%20a%20const%20item/near/177651503" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/inspecting.20ADT.20types.20on.20RHS.20of.20a.20const.20item.html#177651503">(Oct 08 2019 at 19:43)</a>:</h4>
<p>(actually maybe my problem is that  I'm trying to walk the const-expression structure based on a <code>ty::Const</code>/<code>mir::interpret::ConstValue</code>, when what I should be doing is starting from the <code>def_id</code> there ...)</p>



<a name="177652410"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/inspecting%20ADT%20types%20on%20RHS%20of%20a%20const%20item/near/177652410" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/inspecting.20ADT.20types.20on.20RHS.20of.20a.20const.20item.html#177652410">(Oct 08 2019 at 19:53)</a>:</h4>
<p>nonetheless I was wondering if there is anyway to traverse the implicit tree structure of a <code>mir::interpret::ConstValue</code> like that, or if it has all been totally boiled away into <code>Scalar</code>s and <code>ByRef</code>s that no longer carry type info...</p>



<a name="177706827"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/inspecting%20ADT%20types%20on%20RHS%20of%20a%20const%20item/near/177706827" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/inspecting.20ADT.20types.20on.20RHS.20of.20a.20const.20item.html#177706827">(Oct 09 2019 at 12:10)</a>:</h4>
<p>okay yes I think I figured out what to do. (There remain interesting questions; e.g. we may want to adopt different rules for consts from the current crate versus extern crates. But that can be resolved later.)</p>



<a name="177713988"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/inspecting%20ADT%20types%20on%20RHS%20of%20a%20const%20item/near/177713988" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/inspecting.20ADT.20types.20on.20RHS.20of.20a.20const.20item.html#177713988">(Oct 09 2019 at 13:37)</a>:</h4>
<p><span class="user-mention" data-user-id="116083">@pnkfelix</span> which tree structure are you talking about? we have a "value visitor" which might be want you want -- but that is type-guided; there is no tree structure to values in rust if you discard the types.</p>



<a name="177714003"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/inspecting%20ADT%20types%20on%20RHS%20of%20a%20const%20item/near/177714003" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/inspecting.20ADT.20types.20on.20RHS.20of.20a.20const.20item.html#177714003">(Oct 09 2019 at 13:37)</a>:</h4>
<p>Rust only has raw uninterpreted bytes in memory, and you can turn them into a tree by picking a type at which to interpret the bytes.</p>



<a name="177714226"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/inspecting%20ADT%20types%20on%20RHS%20of%20a%20const%20item/near/177714226" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/inspecting.20ADT.20types.20on.20RHS.20of.20a.20const.20item.html#177714226">(Oct 09 2019 at 13:40)</a>:</h4>
<p>yeah, the tree I was thinking of was the HIR for the const definition</p>



<a name="177714236"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/inspecting%20ADT%20types%20on%20RHS%20of%20a%20const%20item/near/177714236" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/inspecting.20ADT.20types.20on.20RHS.20of.20a.20const.20item.html#177714236">(Oct 09 2019 at 13:40)</a>:</h4>
<p>which I eventually did find</p>



<a name="177714271"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/inspecting%20ADT%20types%20on%20RHS%20of%20a%20const%20item/near/177714271" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/inspecting.20ADT.20types.20on.20RHS.20of.20a.20const.20item.html#177714271">(Oct 09 2019 at 13:40)</a>:</h4>
<p>and then reflecting on that has made me realize that, in the most general case, we cannot assume we have that available</p>



<a name="177714273"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/inspecting%20ADT%20types%20on%20RHS%20of%20a%20const%20item/near/177714273" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/inspecting.20ADT.20types.20on.20RHS.20of.20a.20const.20item.html#177714273">(Oct 09 2019 at 13:40)</a>:</h4>
<p>oh, you mean the AST, pre MIR-generation. yeah sorry I couldn't help much there. glad you found the answer somewhere :D</p>



<a name="177714334"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/inspecting%20ADT%20types%20on%20RHS%20of%20a%20const%20item/near/177714334" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/inspecting.20ADT.20types.20on.20RHS.20of.20a.20const.20item.html#177714334">(Oct 09 2019 at 13:41)</a>:</h4>
<p>this const pattern stuff is a pretty amazing rabbit hole.</p>



<a name="177714525"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/inspecting%20ADT%20types%20on%20RHS%20of%20a%20const%20item/near/177714525" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/inspecting.20ADT.20types.20on.20RHS.20of.20a.20const.20item.html#177714525">(Oct 09 2019 at 13:43)</a>:</h4>
<p>I can only guess.</p>



<a name="177714530"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/inspecting%20ADT%20types%20on%20RHS%20of%20a%20const%20item/near/177714530" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/inspecting.20ADT.20types.20on.20RHS.20of.20a.20const.20item.html#177714530">(Oct 09 2019 at 13:43)</a>:</h4>
<p>I have avoided pattern matching code like the plague myself ;)</p>



<a name="177787633"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/inspecting%20ADT%20types%20on%20RHS%20of%20a%20const%20item/near/177787633" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/inspecting.20ADT.20types.20on.20RHS.20of.20a.20const.20item.html#177787633">(Oct 10 2019 at 08:00)</a>:</h4>
<p>Oh yea, the only reasonable way to get at parts of a constant is via the value visitors, they implement all the logic for getting the types of fields and so on</p>



<a name="177794512"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/inspecting%20ADT%20types%20on%20RHS%20of%20a%20const%20item/near/177794512" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/inspecting.20ADT.20types.20on.20RHS.20of.20a.20const.20item.html#177794512">(Oct 10 2019 at 09:50)</a>:</h4>
<p>hmm. Okay maybe I will try to use a value visitor. (I do have a type in hand, so the fact that they are type-guided should not be a problem.)</p>



<a name="177794555"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/inspecting%20ADT%20types%20on%20RHS%20of%20a%20const%20item/near/177794555" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/inspecting.20ADT.20types.20on.20RHS.20of.20a.20const.20item.html#177794555">(Oct 10 2019 at 09:51)</a>:</h4>
<p><span class="user-mention" data-user-id="124288">@oli</span> can you point me at the specific trait definition(s)? And/or example usage of value visitor? I specifically want/need to traverse a value to identify all of the struct/variant constructors that would have been used to create that value.</p>



<a name="177794588"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/inspecting%20ADT%20types%20on%20RHS%20of%20a%20const%20item/near/177794588" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/inspecting.20ADT.20types.20on.20RHS.20of.20a.20const.20item.html#177794588">(Oct 10 2019 at 09:51)</a>:</h4>
<p>or maybe that's not what I actually need, if the visitor handles type lookup and field enumeration...</p>



<a name="177795268"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/inspecting%20ADT%20types%20on%20RHS%20of%20a%20const%20item/near/177795268" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/inspecting.20ADT.20types.20on.20RHS.20of.20a.20const.20item.html#177795268">(Oct 10 2019 at 10:02)</a>:</h4>
<p>(really I just want to traverse a view of the value that exposes its types, so that I can ask each ADT if it implements <code>Structural</code>)</p>



<a name="177795301"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/inspecting%20ADT%20types%20on%20RHS%20of%20a%20const%20item/near/177795301" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/inspecting.20ADT.20types.20on.20RHS.20of.20a.20const.20item.html#177795301">(Oct 10 2019 at 10:03)</a>:</h4>
<p>(and this way you would be allowed to have an <code>enum</code> with some variants that are <em>not</em> <code>Structural</code>, just as long as no pattern uses a const that uses such a variant.)</p>



<a name="177802442"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/inspecting%20ADT%20types%20on%20RHS%20of%20a%20const%20item/near/177802442" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/inspecting.20ADT.20types.20on.20RHS.20of.20a.20const.20item.html#177802442">(Oct 10 2019 at 12:06)</a>:</h4>
<p>Oh, I think I need to provide still more context: I'm trying to incorporate this into code that <em>is</em> currently using <code>tcx.const_eval</code> to evaluate constants, so I end up with <code>ty::Const</code> as my results to inspect. Do the visitors defined in <code>rustc_mir::interpret::visitor</code> handle  that? My cursory inspection indicates that it may want operands that have a bit more structure ...?</p>



<a name="177802507"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/inspecting%20ADT%20types%20on%20RHS%20of%20a%20const%20item/near/177802507" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/inspecting.20ADT.20types.20on.20RHS.20of.20a.20const.20item.html#177802507">(Oct 10 2019 at 12:06)</a>:</h4>
<p>lets see, who implements the <code>rustc::interpret::visitor::Value</code> trait ...</p>



<a name="177802533"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/inspecting%20ADT%20types%20on%20RHS%20of%20a%20const%20item/near/177802533" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/inspecting.20ADT.20types.20on.20RHS.20of.20a.20const.20item.html#177802533">(Oct 10 2019 at 12:07)</a>:</h4>
<p><code>OpTy</code> and <code>MPlaceTy</code> ...</p>



<a name="177802625"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/inspecting%20ADT%20types%20on%20RHS%20of%20a%20const%20item/near/177802625" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pnkfelix <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/inspecting.20ADT.20types.20on.20RHS.20of.20a.20const.20item.html#177802625">(Oct 10 2019 at 12:08)</a>:</h4>
<p>ah it looks like I may be able to feed the <code>ty::Const</code> into <code>eval_const_to_op</code> to get an <code>OpTy</code> I could then visit...</p>



<a name="177833900"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/inspecting%20ADT%20types%20on%20RHS%20of%20a%20const%20item/near/177833900" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/inspecting.20ADT.20types.20on.20RHS.20of.20a.20const.20item.html#177833900">(Oct 10 2019 at 17:38)</a>:</h4>
<p>Yes, converting to an <code>OpTy</code> first should be the right way ^TM This is done frequently for match checking in <a href="https://github.com/rust-lang/rust/blob/9c588c1e2391c20c156814f7ae118f62e96a3976/src/librustc_mir/const_eval.rs#L517" target="_blank" title="https://github.com/rust-lang/rust/blob/9c588c1e2391c20c156814f7ae118f62e96a3976/src/librustc_mir/const_eval.rs#L517">https://github.com/rust-lang/rust/blob/9c588c1e2391c20c156814f7ae118f62e96a3976/src/librustc_mir/const_eval.rs#L517</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>